<?php

namespace App\Exceptions;

use App\Api\Helpers\ApiResponse;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    use ApiResponse;
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param \Throwable $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Throwable $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
        //1、拦截ajax请求的异常
        if ($request->ajax()) {
            //2、其他的异常区别开发环境和生产环境
            if (env('APP_DEBUG')) {
                return $this->failed("file:".$exception->getFile().' line:'.$exception->getLine().' error:'.$exception->getMessage());
            }
            //生产环境隐藏错误
            return $this->internalError('系统内部错误!');
        }
        //非ajax请求先不用管
        return parent::render($request, $exception);
    }
}
